package com.magicborrow.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RatingBar;
import android.widget.LinearLayout;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.activity.HomepageActivity;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.beans.TagBean;
import com.magicborrow.utils.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class StuffInfoFragment extends Fragment {

    private ImageView imvHead;
    private TextView tvName;
    private RatingBar rbPingfen;
    private TextView tvPinfen;
    private TextView stuffName;
    private TextView tvInfo;
    private LinearLayout llTag;
    private TextView tvCollection;
    private RelativeLayout llHead;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.stuff_info_fragment, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        imvHead = (ImageView) view.findViewById(R.id.imv_head);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        rbPingfen = (RatingBar) view.findViewById(R.id.rb_pingfen);
        tvPinfen = (TextView) view.findViewById(R.id.tv_pinfen);
        stuffName = (TextView) view.findViewById(R.id.stuff_name);
        tvInfo = (TextView) view.findViewById(R.id.tv_info);
        llTag = (LinearLayout) view.findViewById(R.id.ll_tag);
        tvCollection = (TextView) view.findViewById(R.id.tv_collection);
        llHead = (RelativeLayout)view.findViewById(R.id.rl_head);
        initdata();

    }

    public void initdata() {
        final StuffBean stuffBean = ((BorrowStuffDetailActivity) getActivity()).getStuff();
        DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(getContext().getResources().getDimensionPixelOffset(R.dimen._30dp))).build();

        ImageLoader.getInstance().displayImage(Constants.ADDRESS + stuffBean.getUser().getAvatar80(),
                imvHead, options);
        tvName.setText(stuffBean.getUser().getNickname());
        rbPingfen.setRating(stuffBean.getUser().getLendScaleAVG());
        tvPinfen.setText("" + StringUtil.double2String(stuffBean.getUser().getLendScaleAVG()) + "（" + stuffBean.getCommentCount() + "人评价）");
        stuffName.setText(stuffBean.getName());
        llTag.removeAllViews();
        if(stuffBean.getTags() != null) {
            for (TagBean tag : stuffBean.getTags()) {
                llTag.addView(createTag(tag.getName()));
            }
        }
        tvInfo.setText(stuffBean.getDescription());
        tvCollection.setText("共" + stuffBean.getFavors() + "人收藏此商品");
        llHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), HomepageActivity.class);
                intent.putExtra("id", stuffBean.getUser().getId());
                getContext().startActivity(intent);
            }
        });

    }

    private TextView createTag(String tag) {
        TextView tv = new TextView(getContext());
        tv.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.tag_bg));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimensionPixelSize(R.dimen.text_size_small_s));
        tv.setTextColor(getContext().getResources().getColor(R.color.orange_red));
        tv.setText("" + tag);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, getContext().getResources().getDimensionPixelOffset(R.dimen._5dp), 0);
        tv.setLayoutParams(params);
        return tv;
    }
}
